/*
 *所有关于PurchaseWarehousing类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*PurchaseWarehousingService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using MyMES.IRepositories;
using MyMes.Infrastructure;
using VOL.Core.Utilities.Response;
using VOL.Entity.DomainModels.Repository;
using MyMES.Dto;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
using VOL.Entity.DomainModels.Core;
using MyMes.Dto;

namespace MyMES.Services
{
    public partial class PurchaseWarehousingService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IBaseRepository<PurchaseWarehousing> _purchaseWarehousingRepo;
        private readonly IBaseRepository<ArrivalNotice> _arrivalNoticeRepo;
        private readonly IBaseRepository<VOL.Entity.DomainModels.Supplier> _supplierRepo;
        private readonly IPurchaseWarehousingRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public PurchaseWarehousingService(
            IPurchaseWarehousingRepository dbRepository,
            IHttpContextAccessor httpContextAccessor,
            IBaseRepository<PurchaseWarehousing> purchaseWarehousingRepo,
            IBaseRepository<ArrivalNotice> arrivalNoticeRepo,
            IBaseRepository<VOL.Entity.DomainModels.Supplier> SupplierRepo
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _purchaseWarehousingRepo = purchaseWarehousingRepo;
            _arrivalNoticeRepo = arrivalNoticeRepo;
            _supplierRepo = SupplierRepo;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        public async Task<PageGridData<PurchaseWarehousingDTO>> GetPurchaseWarehousing(GetPurchaseDTO dto)
        {
            PageGridData<PurchaseWarehousingDTO> pageData = new PageGridData<PurchaseWarehousingDTO>();

            var purchaseWarehousingList = from a in _purchaseWarehousingRepo.GetAll()
                                          join b in _arrivalNoticeRepo.GetAll()
                                        on a.InformCode equals b.NoticeCode
                                          join c in _supplierRepo.GetAll() on b.SupplierCode equals c.SupplierCode
                                          select new PurchaseWarehousingDTO()
                                          {
                                              Id=a.Id,
                                              InboundListCode=a.InboundListCode,
                                              InboundListName=a.InboundListName,
                                              InboundListTime=a.InboundListTime,
                                              InformCode = a.InformCode,
                                              States = a.States,
                                              SupplierCode=b.SupplierCode,
                                              PurchaseOrderCode=b.PurchaseOrderCode,
                                              SupplierName=c.SupplierName
                                          };

            // 使用WhereIf方法进行条件查询
            purchaseWarehousingList = purchaseWarehousingList.WhereIF(!string.IsNullOrEmpty(dto.InboundListCode),
                                 x => x.InboundListCode.Contains(dto.InboundListCode));

            purchaseWarehousingList = purchaseWarehousingList.WhereIF(!string.IsNullOrEmpty(dto.InboundListName),
                               x => x.InboundListName.Contains(dto.InboundListName));

            purchaseWarehousingList = purchaseWarehousingList.WhereIF(!string.IsNullOrEmpty(dto.InformCode),
                               x => x.InformCode.Contains(dto.InformCode));

            purchaseWarehousingList = purchaseWarehousingList.WhereIF(!string.IsNullOrEmpty(dto.SupplierName),
                               x => x.SupplierName.Contains(dto.SupplierName));

            // 日期查询 - 如果有日期查询条件，则查询当天
            if (dto.InboundListTime.HasValue)
            {
                DateTime startDate = dto.InboundListTime.Value.Date;
                DateTime endDate = startDate.AddDays(1).AddSeconds(-1);
                purchaseWarehousingList = purchaseWarehousingList.Where(a => a.InboundListTime >= startDate && a.InboundListTime <= endDate);
            }

            // 获取总记录数
            pageData.total = await purchaseWarehousingList.CountAsync();

            // 分页处理
            var data = await purchaseWarehousingList.OrderByDescending(a => a.InboundListTime)
                                 .Skip((dto.PageIndex - 1) * dto.PageSize)
                                 .Take(dto.PageSize)
                                 .ToListAsync();

            pageData.rows = data;

            return pageData;
        }
        public async Task<WebResponseContent> AddPurchaseWarehousing(PurchaseWarehousing purchase)
        {
            var res = new WebResponseContent();

            var i = await _purchaseWarehousingRepo.AddAsync(purchase);

            res.OK("添加成功", i);

            return await Task.FromResult(res);
        }

        public async Task<WebResponseContent> UpdPurchaseWarehousing(PurchaseWarehousing purchase)
        {
            var res = new WebResponseContent();

            var i = await _purchaseWarehousingRepo.UpdateAsync(purchase);

            res.OK("修改成功", i);

            return await Task.FromResult(res);
        }

        public async Task<WebResponseContent> RemPurchaseWarehousing(string inboundListCode)
        {
            var res = new WebResponseContent();

            var i = await _purchaseWarehousingRepo.DelAll(x => inboundListCode.Contains(x.InboundListCode));

            res.OK("删除", i);

            return await Task.FromResult(res);
        }

        public Task<WebResponseContent> FindPurchaseWarehousing(string inboundListCode)
        {
            var res = new WebResponseContent();

            var purchaseWarehousingList = _purchaseWarehousingRepo.GetAll(x => x.InboundListCode == inboundListCode).FirstOrDefault();

            res.OK("反填成功", purchaseWarehousingList);

            return Task.FromResult(res);
        }
    }
}
